package com.last.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.last.entity.Admin;
import com.last.entity.Result;
import com.last.entity.User;
import com.last.entity.UserDto;
import com.last.jwt.util.JwtUtils;
import com.last.jwt.util.RsaUtils;
import com.last.mapper.AdminMapper;
import com.last.mapper.UserInfoMapper;
import com.last.service.UserService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import javax.annotation.Resource;
import java.security.PrivateKey;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private AdminMapper adminMapper;

    @Override
    public Result login(UserDto userDto) {

        if (userDto == null) {
            return new Result(false,"参数不合法");
        }

        if ("1".equals(userDto.getUserType())){//平台用户
            //根据用户名查询用户信息
            QueryWrapper<User> wrapper = new QueryWrapper<User>();
            wrapper.eq("user_name",userDto.getUsername());
            User user = userInfoMapper.selectOne(wrapper);
            if (user == null) {
                return new Result(false,"用户名或者密码不正确");
            }

            //比较密码（bcrypt）
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            boolean matches = bCryptPasswordEncoder.matches(userDto.getPassword(), user.getPassword());
            if (matches != true) {//密码不正确
                return new Result(false,"密码不正确");
            }


            //颁发令牌
            String token = genJwtToken(user);

            if (token == null) {
                return new Result(false,"用户名不正确");
            }


            return new Result(true,"登录成功",token);
        }



        if("2".equals(userDto.getUserType())){//管理系统

            QueryWrapper<Admin> wrapper = new QueryWrapper<Admin>();
            wrapper.eq("username",userDto.getUsername());
            Admin admin = adminMapper.selectOne(wrapper);
            if (admin == null) {
                return new Result(false,"用户名或者密码不正确");
            }
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            boolean matches = bCryptPasswordEncoder.matches(userDto.getPassword(), admin.getPassword());
            if (!matches) {
                // 密码不正确
                return new Result(false,"用户名或者密码不正确");
            }


            //颁发令牌
            String token = genJwtToken(admin);

            if (token == null) {
                return new Result(false,"用户名或者密码不正确");
            }


            return new Result(true,"登录成功",token);

        }



        if("3".equals(userDto.getUserType())){//自媒体系统

            QueryWrapper<Admin> wrapper = new QueryWrapper<Admin>();
            wrapper.eq("username",userDto.getUsername());
            Admin admin = adminMapper.selectOne(wrapper);



            if (admin == null) {
                return new Result(false,"用户名或者密码不正确");
            }
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            boolean matches = bCryptPasswordEncoder.matches(userDto.getPassword(), admin.getPassword());
            if (!matches) {//密码不正确
                return new Result(false,"用户名或者密码不正确");
            }
            //颁发令牌
            String token = genJwtToken(admin);

            if (token == null) {
                return new Result(false,"用户名或者密码不正确");
            }
            return new Result(true,"登录成功",token);

        }

        return  new Result(false,"用户名或者密码不正确");

    }




    private  String  genJwtToken(Object User){

       try {
           //从类路径中获取私钥
           String privateKeyPath = ResourceUtils.getFile("classpath:ras_private").getPath();
           PrivateKey privateKey = RsaUtils.getPrivateKey(privateKeyPath);

           String jwtToken = JwtUtils.generateTokenExpireInMinutes(User, privateKey, 30);
           System.out.println(jwtToken);

            return jwtToken;

       }catch (Exception e){
               e.printStackTrace();
               return  null;
       }


    }

}